--    Program: 		CAPPOEXT_RECEIVING_OPTION.sql
--    Description:	Extract PO Receiving Option information 
--    Revision:		1st     
--    Parameter: 	1. set of books short name e.g. SOB_C999

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;
alter session set nls_date_format='YYYYMMDD HH24:MI:SS';

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
                    		
   l_utl_file       		utl_file.file_type;
   l_filename       		varchar2(255);
   l_pathname       		varchar2(100);
                    		
   l_ngo_sobid				number;
   l_set_of_books_name	    varchar2(150);
   l_set_of_books_id		number;
   
   cursor cur_financial_option (p_set_of_books_id number) is
      select rp.* 
      from   rcv_parameters rp,
      	     org_organization_definitions ood
      where  rp.organization_id = ood.organization_id
      and    ood.set_of_books_id = p_set_of_books_id;

begin
   l_pathname := '/tmp';
   l_filename := 'CAPPO_RECEIVING_OPTION.dat';
   l_utl_file := utl_file.fopen(l_pathname, l_filename, 'w');
   
   l_processed_count := 0;
   l_rejected_count  := 0;
   
   l_set_of_books_name := '&1';
   
   begin
     select set_of_books_id
     into   l_set_of_books_id
     from   gl_sets_of_books
     where   upper(short_name) = upper(l_set_of_books_name);
   exception
     when others then
       dbms_output.put_line(l_set_of_books_name||': set of books name not found');
   end;      

   for cur in cur_financial_option(l_set_of_books_id) loop   	
     
     -- output data
     utl_file.put_line( l_utl_file, 
       cur.ORGANIZATION_ID                 || chr(9) ||
       cur.LAST_UPDATE_DATE                || chr(9) ||
       cur.LAST_UPDATED_BY                 || chr(9) ||
       cur.CREATION_DATE                   || chr(9) ||
       cur.CREATED_BY                      || chr(9) ||
       cur.LAST_UPDATED_LOGIN              || chr(9) ||
       cur.QTY_RCV_TOLERANCE               || chr(9) ||
       cur.QTY_RCV_EXCEPTION_CODE          || chr(9) ||
       cur.ENFORCE_SHIP_TO_LOCATION_CODE   || chr(9) ||
       cur.ALLOW_EXPRESS_DELIVERY_FLAG     || chr(9) ||
       cur.DAYS_EARLY_RECEIPT_ALLOWED      || chr(9) ||
       cur.DAYS_LATE_RECEIPT_ALLOWED       || chr(9) ||
       cur.RECEIPT_DAYS_EXCEPTION_CODE     || chr(9) ||
       cur.RECEIVING_ROUTING_ID            || chr(9) ||
       cur.ALLOW_SUBSTITUTE_RECEIPTS_FLAG  || chr(9) ||
       cur.ALLOW_UNORDERED_RECEIPTS_FLAG   || chr(9) ||
       cur.BLIND_RECEIVING_FLAG            || chr(9) ||
       cur.REQUEST_ID                      || chr(9) ||
       cur.PROGRAM_APPLICATION_ID          || chr(9) ||
       cur.PROGRAM_ID                      || chr(9) ||
       cur.PROGRAM_UPDATE_DATE             || chr(9) ||
       cur.RECEIVING_ACCOUNT_ID            || chr(9) ||
       cur.ALLOW_CASCADE_TRANSACTIONS      || chr(9) ||
       cur.GLOBAL_ATTRIBUTE1               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE2               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE3               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE4               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE5               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE6               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE7               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE8               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE9               || chr(9) ||
       cur.GLOBAL_ATTRIBUTE10              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE11              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE12              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE13              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE14              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE15              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE16              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE17              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE18              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE19              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE20              || chr(9) ||
       cur.GLOBAL_ATTRIBUTE_CATEGORY       || chr(9) ||
       cur.RECEIPT_ASN_EXISTS_CODE         || chr(9) ||
       cur.USER_DEFINED_RECEIPT_NUM_CODE   || chr(9) ||
       cur.MANUAL_RECEIPT_NUM_TYPE         || chr(9) ||
       cur.NEXT_RECEIPT_NUM
	 );
                                             
	 l_processed_count := l_processed_count + 1;
   end loop;

   if l_processed_count = 0 then   
     l_rejected_count := l_rejected_count + 1;
     l_err_msg := l_set_of_books_name || ': Receiving Option not found';
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPO_RECEIVING_OPTION', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;

   utl_file.fclose(l_utl_file);
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
